﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class Sach_DAO
    {
        clsDocSQL m_clsDocSQL = new clsDocSQL();

        protected object Getdatarow(DataTable dt, int dong)
        {
            Sach_DTO dto = new Sach_DTO();
            dto.Str_MaSach = dt.Rows[dong]["MaSach"].ToString();
            dto.Str_TenSach = dt.Rows[dong]["TenSach"].ToString();
            dto.D_GiaMua = decimal.Parse(dt.Rows[dong]["GiaMua"].ToString());
            dto.D_GiaBan = decimal.Parse(dt.Rows[dong]["GiaBan"].ToString());
            dto.Str_MaNXB = dt.Rows[dong]["MaNXB"].ToString();
            dto.Str_MaTacGia = dt.Rows[dong]["MaTacGia"].ToString();
            dto.Str_MaTheLoai = dt.Rows[dong]["MaTheLoai"].ToString();
            dto.I_SoLuong = int.Parse(dt.Rows[dong]["SoLuong"].ToString());
            dto.I_SoLuongBan = int.Parse(dt.Rows[dong]["SoLuongBan"].ToString());
            dto.I_TrangThai = int.Parse(dt.Rows[dong]["TrangThai"].ToString());
            return (object)dto;
        }
        public Sach_DTO[] DanhSachSach()
        {
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure("sp_LayDanhSachSach");
            Sach_DTO[] arr;
            arr = new Sach_DTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object obj = Getdatarow(dt, i);
                arr[i] = (Sach_DTO)obj;
            }
            return arr;
        }
        public Sach_DTO DanhSachSach_TheoMaSach(String str_MaSach)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[1];
            m_SqlParameter[0] = new SqlParameter("@MaSach", str_MaSach);
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure1("sp_LayDanhSachSach_TheoMaSach", m_SqlParameter);
            Sach_DTO arr = new Sach_DTO();
            object tg = Getdatarow(dt, 0);
            arr = (Sach_DTO)tg;
            return arr;
        }
        public String MaSachTang()
        {
            String kq = "";
            kq = m_clsDocSQL.DocKieuString("MaSach_Tang");
            return kq.ToString();
        }
        public int ThemSach(Sach_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[11];
            m_SqlParameter[0] = new SqlParameter("@MaSach", dto.Str_MaSach);
            m_SqlParameter[1] = new SqlParameter("@TenSach", dto.Str_TenSach);
            m_SqlParameter[2] = new SqlParameter("@GiaMua", dto.D_GiaMua);
            m_SqlParameter[3] = new SqlParameter("@GiaBan", dto.D_GiaBan);
            m_SqlParameter[4] = new SqlParameter("@MaNXB", dto.Str_MaNXB);
            m_SqlParameter[5] = new SqlParameter("@MaTacGia", dto.Str_MaTacGia);
            m_SqlParameter[6] = new SqlParameter("@MaTheLoai", dto.Str_MaTheLoai);
            m_SqlParameter[7] = new SqlParameter("@SoLuong", dto.I_SoLuong);
            m_SqlParameter[8] = new SqlParameter("@SoLuongBan", dto.I_SoLuongBan);
            m_SqlParameter[9] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[10] = new SqlParameter("@KQ", 0);
            m_SqlParameter[10].Direction = ParameterDirection.Output;
            int KQ = m_clsDocSQL.Insert_Delete_Update("sp_ThemSach", m_SqlParameter);
            return KQ;
        }
        public int XoaSach(Sach_DTO dto)//
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[2];
            m_SqlParameter[0] = new SqlParameter("@MaSach", dto.Str_MaSach);
            m_SqlParameter[1] = new SqlParameter("@KQ", 0);
            m_SqlParameter[1].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_XoaSach", m_SqlParameter);
            return kq;
        }

        public int CapNhatSach(Sach_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[11];
            m_SqlParameter[0] = new SqlParameter("@MaSach", dto.Str_MaSach);
            m_SqlParameter[1] = new SqlParameter("@TenSach", dto.Str_TenSach);
            m_SqlParameter[2] = new SqlParameter("@GiaMua", dto.D_GiaMua);
            m_SqlParameter[3] = new SqlParameter("@GiaBan", dto.D_GiaBan);
            m_SqlParameter[4] = new SqlParameter("@MaNXB", dto.Str_MaNXB);
            m_SqlParameter[5] = new SqlParameter("@MaTacGia", dto.Str_MaTacGia);
            m_SqlParameter[6] = new SqlParameter("@MaTheLoai", dto.Str_MaTheLoai);
            m_SqlParameter[7] = new SqlParameter("@SoLuong", dto.I_SoLuong);
            m_SqlParameter[8] = new SqlParameter("@SoLuongBan", dto.I_SoLuongBan);
            m_SqlParameter[9] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[10] = new SqlParameter("@KQ", 0);
            m_SqlParameter[10].Direction = ParameterDirection.Output;
            int KQ = m_clsDocSQL.Insert_Delete_Update("sp_CapNhatSach", m_SqlParameter);
            return KQ;
        }
    }
}
